<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\admin\model\User as UserModel;

<<<<<<< HEAD

=======
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
class Index extends Controller
{
    /**
     * 后台登录页面
     * @return \think\response\View
     */
    public function index()
    {
<<<<<<< HEAD
=======
        // echo 222;die;
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
        return view('admin@index/login');
    }

    /**
     * 后台登录处理
     * @return \think\response\View
     */
    public function logindo(Request $request)
    {
        $post = input('post.');
        // var_dump($post); die;
        $username = $post['username'];
        $userpass = $post['userpass'];
        // $user = new UserModel;
        // $user->data($post);
        $result = db('user')->where(['username' => $username])->find();
        // var_dump($result['status']); exit;
        // 验证该用户是否有效
        if ($result['status'] !== 1) {
            $this->error('该用户无效!');
            exit;
        }

<<<<<<< HEAD
        if (!$result) {
=======
        // var_dump($result); exit;
    public function logindo()
    {   
        //接收用户名和密码
        $username = input('username');
        $password = input('password');
        // var_dump($username,$password);die;
        Session::clear();
        Session::clear('think');
        // var_dump($_SESSION);die;
        //验证
        // $user = M('User');
        $data = db('user_test')->where(array('name'=>$username))->find();
        // var_dump($data);die;
        if (!$data) {
>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
            $this->error('用户名不存在！');
            exit;
        }
        //验证密码
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
        if ($result['userpass'] != md5($userpass)) {
            $this->error('密码不正确');
            exit;
        }

        //把用户信息添加到session
        Session::set('admin_user', $result);
        // var_dump(Session::get('admin_user')); exit;
        
        //根据用户id获取对应的节点信息
        $sql = "select n.mname, n.aname from dj_user u, dj_user_role ur, dj_role r, dj_role_node rn, dj_node n where ur.rid = r.id && r.id = rn.rid && rn.nid = n.id && ur.uid = ".$result['id'];
        $list = DB::query($sql);
        // var_dump($list); exit;

        //控制器名转换为大写
        foreach ($list as $key => $val) {
            $list[$key]['mname'] = ucfirst($val['mname']);
        }
        // var_dump($list[$key]['mname']);

        $nodelist = array();
        $nodelist['Index'] = array('index');
        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['mname']][] = $v['aname'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['aname']=="edit"){
                $nodelist[$v['mname']][]="save";
            }
            if($v['aname']=="add"){
                $nodelist[$v['mname']][]="doadd";
            }
        }

        //将权限信息放置到session中
        Session::set('admin_user_nodelist', $nodelist);
        // var_dump(Session::get('admin_user_nodelist'));
        // exit;
        return $this->redirect('admin/main/index');

<<<<<<< HEAD
=======
=======
        if ($data['password'] != md5($password)) {
            $this->error('密码不正确');
            exit;
        }
        //把用户信息添加到session
        Session::set('admin_user',$data); 

        // setSession::
        // var_dump($_SESSION);die;

        //根据用户id获取对应的节点信息
        //$sql = "select n.controllername,n.functionname from lamp_user u join lamp_user_role ur on u.id=ur.uid join lamp_role_node rn on ur.rid=rn.rid join lamp_node n on rn.nid=n.id where u.id={$users['id']}";
        //$list = M()->query($sql);

        $list = db('node_test')->field('controllername,functionname')->where('id in'.db('role_node_test')->field('nid')->where("rid in ".db('user_role_test')->field('rid')->where(array('uid'=>array('eq',$data['id'])))->buildSql())->buildSql())->select();
        // var_dump($list);die;
        
        //控制器名转换为大写
        foreach ($list as $key => $val) {
            $list[$key]['controllername'] = ucfirst($val['controllername']);
        }

        //查看查询出来的信息
        // var_dump($list);die;


        $nodelist = array();
        $nodelist['Index'] = array('index');

        //遍历重新拼装
        foreach($list as $v){
            $nodelist[$v['controllername']][] = $v['functionname'];
            //把修改和执行修改 添加和执行添加 拼装到一起
            if($v['functionname']=="edit"){
                $nodelist[$v['controllername']][]="save";
            }
            if($v['functionname']=="add"){
                $nodelist[$v['controllername']][]="doadd";
            }
        }
        // var_dump($nodelist);die;
        
        //将权限信息放置到session中
        Session::set('admin_user[nodelist]',$nodelist);
        // var_dump(Session::get('admin_user[nodelist]'));die;

        //重组的信息
        // var_dump($_SESSION['admin_user']);exit;

        //跳转到首页
        $this->redirect('admin/main/index');
>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
    }

    /**
     * 后台登出处理
     * @return \think\response\View
     */
    public function logout()
    {
<<<<<<< HEAD
        Session::delete('admin_user');
=======
<<<<<<< HEAD
        unset($_SESSION['admin_user']);
=======
        // echo 111;die;
>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
        $this->redirect('admin/index/index');
    }

}
<<<<<<< HEAD
=======
<<<<<<< HEAD
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b





<<<<<<< HEAD
=======
=======
>>>>>>> 2042d0c0398cdc6116be32c9963026791e40783c
>>>>>>> a40bd7c37eef93aca4b7ae3126fd10da65b4232b
